﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Resn.WhuPub.BLL;

namespace Resn.WhuPub.WPFClientUI
{
        /// <summary>
        /// CreateContactWindow.xaml 的交互逻辑
        /// </summary>
        public partial class CreateContactWindow
        {
                private readonly GlobalManager _gm = GlobalManager.Instance;

                public CreateContactWindow()
                {
                        InitializeComponent();
                }

                private void Window_Loaded(object sender, RoutedEventArgs e)
                {
                        LayoutRoot.DataContext = _gm;
                }

                private void Window_OnMouseDown(object sender, MouseButtonEventArgs e)
                {
                        if (e.ChangedButton == MouseButton.Left)
                        {
                                DragMove();
                        }
                }

                private void AcceptButton_Click(object sender, RoutedEventArgs e)
                {
                        if (String.IsNullOrEmpty(_gm.Name))
                        {
                                MessageBox.Show("名称不能为空");
                                return;
                        }


                        try
                        {
                                Contact contact = _gm.Department.CreateContact(_gm.Name, 
                                                                               _gm.Title, 
                                                                               _gm.Duty, 
                                                                               _gm.Telephone, 
                                                                               _gm.QQ, 
                                                                               _gm.Email, 
                                                                               _gm.IsOwnAuthor,
                                                                               _gm.Memo);
                                contact.Insert();

                                _gm.Contacts.Add(contact);
                        }
                        catch (Exception ex)
                        {
                                MessageBox.Show(ex.Message);
                                return;
                        }

                        finally
                        {
                        }

                        Close();

                }


                
        }
}